﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Skipper_Connector
{
    public partial class Form2 : Form
    {
        COMHelper myCOMs = new COMHelper();
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            foreach (string strCOM in myCOMs.GetCOM())
            {
                comboBox1.Items.Add(strCOM);
                comboBox2.Items.Add(strCOM);
            }
            comboBox1.SelectedItem = Config.InPort;
            comboBox2.SelectedItem = Config.OutPort;
            comboBox3.SelectedItem = Config.NPAN;
            comboBox4.SelectedItem = Config.PMOD;
            textBox1.Text = Config.CodePath;
            textBox2.Text = Config.MachinePath;
            textBox3.Text = Config.Toolfile;
            lastToolBox.Text = Config.LastToolFile;
            ServerPathBox.Text = Config.DataPath;
            CharBox.Text = Config.FlagChar;
            JobIndexBox.Text = Config.JobNameIndex.ToString();
            CodeIndexbox.Text = Config.CodeNameIndex.ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Config.InPort = comboBox1.SelectedItem.ToString();
                Config.OutPort = comboBox2.SelectedItem.ToString();
                Config.NPAN = comboBox3.SelectedItem.ToString();
                Config.PMOD = comboBox4.SelectedItem.ToString();
                Config.CodePath = textBox1.Text;
                Config.MachinePath = textBox2.Text;
                Config.DataPath = ServerPathBox.Text;
                Config.Toolfile = textBox3.Text;
                Config.LastToolFile = lastToolBox.Text;
                Config.CurrentToolFile = textBox3.Text;
                SaveConfig();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog codePath = new FolderBrowserDialog();
            if (codePath.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = codePath.SelectedPath;
                Config.CodePath = textBox1.Text;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog machinePath = new FolderBrowserDialog();
            if (machinePath.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = machinePath.SelectedPath;
                Config.MachinePath = textBox2.Text;
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            FileDialog toolfile = new OpenFileDialog();
            toolfile.Filter = "刀具文件|*.tlfx";
            if (toolfile.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = toolfile.FileName;
                Config.Toolfile = textBox3.Text;
            }
        }
        private void SaveConfig()
        {
            int jobinex;
            int codeindex;

            if (CharBox.Text.Length < 2 && int.TryParse(JobIndexBox.Text, out jobinex) && int.TryParse(CodeIndexbox.Text, out codeindex))
            {
                Config.FlagChar = CharBox.Text;
                Config.JobNameIndex = int.Parse(JobIndexBox.Text);
                Config.CodeNameIndex = int.Parse(CodeIndexbox.Text);
                Config.OpenCheckRepeat = CheckRepeatbox.Checked;
            }
        }

        private void OpenToolFileBT_Click(object sender, EventArgs e)
        {
            FileDialog toolfile = new OpenFileDialog();
            toolfile.Filter = "刀具文件|*.tlfx";
            if (toolfile.ShowDialog() == DialogResult.OK)
            {
                lastToolBox.Text = toolfile.FileName;
                Config.LastToolFile = lastToolBox.Text;
            }
        }

        private void ServerPathBt_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog serverPath = new FolderBrowserDialog();
            if (serverPath.ShowDialog() == DialogResult.OK)
            {
                ServerPathBox.Text = serverPath.SelectedPath;
                Config.DataPath = ServerPathBox.Text;
            }
        }
    }
}
